@svizzle/utils/reduceCb[any-any]-[array-any]

Methods

(static) reduceFromEmptyArray(function) → {function}

Source:
Since:
  • 0.3.0

Return a reduce function expecting an array to reduce with the passed reducer with an empty array as the initial value

Example
> reduce = reduceFromEmptyArray((acc, x) => {
  return acc.slice(-2).concat([x.value]);
})
> reduce([
  {a: 1, value: 2},
  {a: 1, value: 3},
  {a: 1, value: 0},
  {a: 1, value: 4},
  {a: 1, value: 7}
])
[0, 4, 7]
Parameters:
Name Type Description
function function

reducer

Returns:
  • Array -> Any
Type
function

(static) reduceFromEmptyObject(function) → {function}

Source:
Since:
  • 0.3.0

Return a reduce function expecting an array to reduce with the passed reducer with an empty object as the initial value

Example
> reduce = reduceFromEmptyObject((acc, x) => {
  acc[x.id] = x.name;
  return acc;
})
> reduce([
  {id: '00', name: 'a'},
  {id: '11', name: 'b'}
])
{11: 'b', 00: 'a'}
Parameters:
Name Type Description
function function

reducer

Returns:
  • Array -> Any
Type
function